home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1812 / 1812.xpi / chrome / colt.jar / content / options.js < prev    next >
Text File  |  2009-08-18  |  11KB  |  329 lines

  1. var objCoLTOptions = {
  2.     CustomFormatLabel : null,
  3.     CustomFormatFormat : null,
  4.     CustomFormatRichText : null,
  5.     
  6.     RichTextFormatLabel : "{RT}",
  7.         
  8.     LoadOptions: function()
  9.     {
  10.         const branch = objCoLT.PrefBranch;
  11.     
  12.         document.getElementById("CLT-Opt-DisplayCopyText").checked = branch.getBoolPref(objCoLT.PrefName_ShowCopyText);
  13.         document.getElementById("CLT-Opt-DisplayCopyBoth").checked    = branch.getBoolPref(objCoLT.PrefName_ShowCopyBoth);
  14.         document.getElementById("CLT-Opt-DisplayCopyPage").checked    = branch.getBoolPref(objCoLT.PrefName_ShowCopyPage);
  15.     
  16.         var count = branch.getIntPref(objCoLT.PrefName_CustomFormatCount);
  17.         for(var i=1; i <= count; i++)
  18.         {
  19.             var labelPref = "custom." + i + ".label";
  20.             var formatPref = "custom." + i + ".format";
  21.             var separatorPref = "custom." + i + ".separator";
  22.             var richTextPref = "custom." + i + ".richtext";
  23.     
  24.             var label = "";
  25.             var format = "";
  26.             var separator = false;
  27.             var richText = false;
  28.     
  29.             if(objCoLT.IsPreferenceSet(labelPref))
  30.                 label = branch.getCharPref(labelPref);
  31.     
  32.             if(objCoLT.IsPreferenceSet(formatPref))
  33.                 format = branch.getCharPref(formatPref);
  34.     
  35.             if(objCoLT.IsPreferenceSet(separatorPref))
  36.                 separator = branch.getBoolPref(separatorPref);
  37.     
  38.             if(objCoLT.IsPreferenceSet(richTextPref))
  39.                 richText = branch.getBoolPref(richTextPref);
  40.             
  41.             var listItem = document.createElement("listitem");
  42.             var listBox = document.getElementById("CLT-Opt-Custom-Format-List");
  43.     
  44.             if(separator)
  45.             {
  46.                 var separatorElement = document.createElement("separator");
  47.                 separatorElement.setAttribute("class", "groove");
  48.                 listItem.appendChild(separatorElement);
  49.     
  50.                 separatorElement = document.createElement("separator");
  51.                 separatorElement.setAttribute("class", "groove");
  52.                 listItem.appendChild(separatorElement);
  53.     
  54.                 listBox.appendChild(listItem);
  55.             }
  56.             else
  57.             {
  58.                 var listCell = document.createElement("listcell");
  59.     
  60.                 listCell.setAttribute("label", label);
  61.                 listItem.appendChild(listCell);
  62.     
  63.                 listCell = document.createElement("listcell");
  64.                 if(richText)
  65.                     listCell.setAttribute("label", this.RichTextFormatLabel);
  66.                 else
  67.                     listCell.setAttribute("label", format);
  68.                 
  69.                 listItem.appendChild(listCell);
  70.                 listBox.appendChild(listItem);
  71.             }
  72.         }
  73.         
  74.         this.UpdateSubmenuControls();
  75.     },
  76.     
  77.     OnAddCustomFormat: function()
  78.     {
  79.         window.openDialog("chrome://colt/content/custom_format.xul", "colt-custom-format-dialog", "centerscreen,chrome,modal", "add");
  80.     
  81.         if(this.CustomFormatLabel && (this.CustomFormatFormat || this.CustomFormatRichText))
  82.         {
  83.             var listCell = document.createElement("listcell");
  84.             var listItem = document.createElement("listitem");
  85.             var listBox = document.getElementById("CLT-Opt-Custom-Format-List");
  86.     
  87.             listCell.setAttribute("label", this.CustomFormatLabel);
  88.             listItem.appendChild(listCell);
  89.     
  90.             listCell = document.createElement("listcell");
  91.             if(this.CustomFormatRichText)
  92.                 listCell.setAttribute("label", this.RichTextFormatLabel);
  93.             else
  94.                 listCell.setAttribute("label", this.CustomFormatFormat);
  95.             
  96.             listItem.appendChild(listCell);
  97.             listBox.selectItem(listBox.appendChild(listItem));
  98.             listBox.ensureElementIsVisible(listBox.selectedItem);
  99.         }
  100.     },
  101.  
  102.     OnAddSeparator: function()
  103.     {
  104.         var listBox = document.getElementById("CLT-Opt-Custom-Format-List");
  105.         var listItem = document.createElement("listitem");
  106.         var separator = document.createElement("separator");
  107.     
  108.         separator.setAttribute("class", "groove");
  109.         listItem.appendChild(separator);
  110.     
  111.         separator = document.createElement("separator");
  112.         separator.setAttribute("class", "groove");
  113.         listItem.appendChild(separator);
  114.     
  115.         listBox.selectItem(listBox.appendChild(listItem));
  116.         listBox.ensureElementIsVisible(listBox.selectedItem);
  117.     },
  118.  
  119.     OnEditCustomFormat: function()
  120.     {
  121.         var listBox = document.getElementById("CLT-Opt-Custom-Format-List");
  122.         var selectedItem = listBox.selectedItem;
  123.     
  124.         if(selectedItem)
  125.         {
  126.             var selectedCell = selectedItem.childNodes[0];
  127.             if(selectedCell.tagName != "separator")
  128.             {
  129.                 window.openDialog("chrome://colt/content/custom_format.xul", "colt-custom-format-dialog",
  130.                                   "centerscreen,chrome,modal", "edit", selectedCell.getAttribute("label"),
  131.                                   selectedItem.childNodes[1].getAttribute("label"));
  132.     
  133.                 if(this.CustomFormatLabel && this.CustomFormatFormat)
  134.                 {
  135.                     var childElements = selectedItem.childNodes;
  136.                     while(childElements.length > 0)
  137.                     {
  138.                         selectedItem.removeChild(childElements[0]);
  139.                     }
  140.                     
  141.                     var listCell = document.createElement("listcell");
  142.     
  143.                     listCell.setAttribute("label", this.CustomFormatLabel);
  144.                     selectedItem.appendChild(listCell);
  145.     
  146.                     listCell = document.createElement("listcell");
  147.                     listCell.setAttribute("label", this.CustomFormatFormat);
  148.                     selectedItem.appendChild(listCell);
  149.                     listBox.ensureElementIsVisible(selectedItem);
  150.                 }
  151.             }
  152.         }
  153.     },
  154.     
  155.     OnListBoxSelected: function()
  156.     {
  157.         var listBox = document.getElementById("CLT-Opt-Custom-Format-List");
  158.         var selectedIndex = listBox.selectedIndex;
  159.     
  160.         var addButton = document.getElementById("CLT-Opt-Add-Format");
  161.         var editButton = document.getElementById("CLT-Opt-Edit-Format");
  162.         var removeButton = document.getElementById("CLT-Opt-Remove-Format");
  163.         var moveUpButton = document.getElementById("CLT-Opt-Move-Format-Up");
  164.         var moveDownButton = document.getElementById("CLT-Opt-Move-Format-Down");
  165.     
  166.         if(listBox.selectedItem)
  167.         {
  168.             removeButton.disabled = false;
  169.     
  170.             if(listBox.selectedItem.childNodes[0].tagName != "separator")
  171.                 editButton.disabled = false;
  172.             else
  173.                 editButton.disabled = true;
  174.     
  175.             if(selectedIndex == 0)
  176.                 moveUpButton.disabled = true;
  177.             else
  178.                 moveUpButton.disabled = false;
  179.     
  180.             if(selectedIndex == listBox.getRowCount() - 1)
  181.                 moveDownButton.disabled = true;
  182.             else
  183.                 moveDownButton.disabled = false;
  184.         }
  185.         else
  186.         {
  187.             editButton.disabled = true;
  188.             removeButton.disabled = true;
  189.             moveUpButton.disabled = true;
  190.             moveDownButton.disabled = true;
  191.         }
  192.     
  193.         if(listBox.getRowCount() == 1)
  194.             removeButton.disabled = true;
  195.     },
  196.     
  197.     OnMoveDown: function()
  198.     {
  199.         var listBox = document.getElementById("CLT-Opt-Custom-Format-List");
  200.         var selItem = listBox.selectedItem;
  201.     
  202.         if(selItem && listBox.selectedIndex != listBox.getRowCount() - 1)
  203.             listBox.selectItem(listBox.insertBefore(selItem, listBox.getNextItem(selItem, 2)));
  204.     },
  205.     
  206.     OnMoveUp: function()
  207.     {
  208.         var listBox = document.getElementById("CLT-Opt-Custom-Format-List");
  209.         var selItem = listBox.selectedItem;
  210.     
  211.         if(selItem && listBox.selectedIndex != 0)
  212.             listBox.selectItem(listBox.insertBefore(selItem, listBox.getPreviousItem(selItem, 1)));
  213.     },
  214.     
  215.     OnRemoveCustomFormat: function()
  216.     {
  217.         var listBox = document.getElementById("CLT-Opt-Custom-Format-List");
  218.         var selItem = listBox.selectedItem;
  219.     
  220.         if(selItem)
  221.             listBox.removeChild(selItem);
  222.     },
  223.  
  224.     SaveOptions: function()
  225.     {
  226.         const branch = objCoLT.PrefBranch;
  227.  
  228.         branch.setBoolPref(objCoLT.PrefName_ShowCopyText, document.getElementById("CLT-Opt-DisplayCopyText").checked);
  229.         branch.setBoolPref(objCoLT.PrefName_ShowCopyBoth, document.getElementById("CLT-Opt-DisplayCopyBoth").checked);
  230.         branch.setBoolPref(objCoLT.PrefName_ShowCopyPage, document.getElementById("CLT-Opt-DisplayCopyPage").checked);
  231.     
  232.         // Clean up all the existing custom formats
  233.         var count = branch.getIntPref(objCoLT.PrefName_CustomFormatCount);
  234.         
  235.         for(var i=1; i <= count; i++)
  236.         {
  237.             var labelPref = "custom." + i + ".label";
  238.             var formatPref = "custom." + i + ".format";
  239.             var separatorPref = "custom." + i + ".separator";
  240.             var richTextPref = "custom." + i + ".richtext";
  241.     
  242.             if(objCoLT.IsPreferenceSet(labelPref))
  243.                 branch.clearUserPref(labelPref);
  244.     
  245.             if(objCoLT.IsPreferenceSet(formatPref))
  246.                 branch.clearUserPref(formatPref);
  247.     
  248.             if(objCoLT.IsPreferenceSet(separatorPref))
  249.                 branch.clearUserPref(separatorPref);
  250.     
  251.             if(objCoLT.IsPreferenceSet(richTextPref))
  252.                 branch.clearUserPref(richTextPref);
  253.         }
  254.     
  255.         // Now store all the current formats
  256.         var listBox = document.getElementById("CLT-Opt-Custom-Format-List");
  257.         for(var i=1; i <= listBox.getRowCount(); i++)
  258.         {
  259.             var listItem = listBox.getItemAtIndex(i - 1);
  260.             var listCell = listItem.childNodes[0];
  261.     
  262.             if(listCell.tagName == "separator")
  263.             {
  264.                 branch.setBoolPref("custom." + i + ".separator", true);
  265.             }
  266.             else
  267.             {
  268.                 var formatLabel = listItem.childNodes[1].getAttribute("label");
  269.                 
  270.                 branch.setCharPref("custom." + i + ".label", listCell.getAttribute("label"));
  271.                 branch.setCharPref("custom." + i + ".format", formatLabel);
  272.                 branch.setBoolPref("custom." + i + ".separator", false);
  273.     
  274.                 if(formatLabel == this.RichTextFormatLabel)
  275.                     branch.setBoolPref("custom." + i + ".richtext", true);
  276.                 else
  277.                     branch.setBoolPref("custom." + i + ".richtext", false);
  278.             }
  279.         }
  280.     
  281.         branch.setIntPref(objCoLT.PrefName_CustomFormatCount, listBox.getRowCount());
  282.         
  283.         try
  284.         {
  285.             var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  286.             var e = wm.getEnumerator("navigator:browser");
  287.             var win;
  288.     
  289.             while(e.hasMoreElements())
  290.             {
  291.                 win = e.getNext();
  292.                 win.objCoLT.OptionsHaveUpdated();
  293.             }
  294.         } catch (e) { objCoLT.Log("Exception caught while propagating options."); }
  295.     },
  296.     
  297.     Trim: function(text)
  298.     {
  299.         if(text == "")
  300.             return "";
  301.     
  302.         text = text.replace(/^\s+/, '');
  303.         text = text.replace(/\s+$/, '');
  304.         return text;
  305.     },
  306.     
  307.     UpdateSubmenuControls: function()
  308.     {
  309.         var disabled = true;
  310.         if(document.getElementById("CLT-Opt-DisplayCopyBoth").checked ||
  311.            document.getElementById("CLT-Opt-DisplayCopyPage").checked)
  312.             disabled = false;
  313.     
  314.         if(disabled)
  315.             document.getElementById("CLT-Opt-Custom-Format-List").selectedIndex = -1;
  316.     
  317.         document.getElementById("CLT-Opt-Custom-Format-List").disabled = disabled;
  318.         document.getElementById("CLT-Opt-Move-Format-Up").disabled = disabled;
  319.         document.getElementById("CLT-Opt-Move-Format-Down").disabled = disabled;
  320.         document.getElementById("CLT-Opt-Add-Format").disabled = disabled;
  321.         document.getElementById("CLT-Opt-Edit-Format").disabled = disabled;
  322.         document.getElementById("CLT-Opt-Remove-Format").disabled = disabled;
  323.         document.getElementById("CLT-Opt-Add-Separator").disabled = disabled;
  324.     
  325.         this.OnListBoxSelected();
  326.     }
  327. };
  328.  
  329.